if(!us) var us={};
if(!us.endrizzi)us.endrizzi={};
if(!us.endrizzi.xdebugassistant) us.endrizzi.xdebugassistant={};

us.endrizzi.xdebugassistant = {
	state_idekey: 'netbeans-xdebug',
	logger: Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService),
	prefManager: Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.xdebugassistant."),
		
	start: function () {
		us.endrizzi.xdebugassistant.state_idekey = us.endrizzi.xdebugassistant.prefManager.getCharPref("extensions.xdebugassistant.idekey");
		us.endrizzi.xdebugassistant.logger.logStringMessage(us.endrizzi.xdebugassistant.state_idekey);
		
		var doc= window.content.document;
		us.endrizzi.xdebugassistant.setCookie(doc,'XDEBUG_SESSION', us.endrizzi.xdebugassistant.state_idekey, 60);
	},
	
	startProfile: function () {
		var doc= window.content.document;
		us.endrizzi.xdebugassistant.setCookie(doc,'XDEBUG_PROFILE',1, 60);
	},
	
	stop: function (){	
		var doc= window.content.document;
		us.endrizzi.xdebugassistant.setCookie(doc,'XDEBUG_SESSION',null, -60);
	},
	
	stopProfile: function () {
		var doc= window.content.document;
		us.endrizzi.xdebugassistant.setCookie(doc,'XDEBUG_PROFILE',null, -60);
	},
	
	eventDispatcher : function(anEvent) {
		var targetId = anEvent.target.id;
		if (targetId == 'xdebugassistant_debug' || targetId == 'xdebugassistant_debug_menuitem'){
			us.endrizzi.xdebugassistant.toggle();
		}else{
			us.endrizzi.xdebugassistant.toggleProfile();
		}
	},
	
	toggle: function() {
		var doc= window.content.document;
		var isEnabled = (us.endrizzi.xdebugassistant.getCookie(doc,'XDEBUG_SESSION') == us.endrizzi.xdebugassistant.state_idekey);
		if (isEnabled) {
			us.endrizzi.xdebugassistant.stop();
		} else {
			us.endrizzi.xdebugassistant.start();
		}
		us.endrizzi.xdebugassistant.refresh();
	},

	toggleProfile: function() {
		var doc= window.content.document;
		var isEnabled = (us.endrizzi.xdebugassistant.getCookie(doc,'XDEBUG_PROFILE')==1);
		if (isEnabled) {
			us.endrizzi.xdebugassistant.stopProfile();
		} else {
			us.endrizzi.xdebugassistant.startProfile();
		}
		us.endrizzi.xdebugassistant.refresh();
	},
	
	refresh: function() {
		// us.endrizzi.xdebugassistant.logger.logStringMessage("Refresh Called");
		var doc= window.content.document;
		
		// Take care of the debugger stuff
		var isEnabled = (us.endrizzi.xdebugassistant.getCookie(doc,'XDEBUG_SESSION') == us.endrizzi.xdebugassistant.state_idekey);
		var button = document.getElementById('xdebugassistant_debug');
		var debugMenuitem = document.getElementById('xdebugassistant_debug_menuitem');
		
		if(isEnabled){
			button.setAttribute("class", "toolbarbutton-1 chromeclass-toolbar-additional debug-on-button");
			debugMenuitem.setAttribute("checked", "true");
			button.tooltiptext="Stop xdebug session";
		}else{
			button.setAttribute("class", "toolbarbutton-1 chromeclass-toolbar-additional debug-off-button");
			debugMenuitem.setAttribute("checked", "false");
			button.tooltiptext="Start xdebug session";
		}
		
		// Take care of the profiler menu item
		var isProfilerEnabled = (us.endrizzi.xdebugassistant.getCookie(doc,'XDEBUG_PROFILE')==1);
		var profilerMenuitem = document.getElementById('xdebugassistant_profile_menuitem');
		
		if (isProfilerEnabled) {
			profilerMenuitem.setAttribute("checked", "true");
		}else{
			profilerMenuitem.setAttribute("checked", "false");
		}
	},
	
	init: function (){
		// Set the state idekey
		us.endrizzi.xdebugassistant.state_idekey = us.endrizzi.xdebugassistant.prefManager.getCharPref("extensions.xdebugassistant.idekey");  
		
		// Refresh on load events (not sure if the is necessary with what is below)
		getBrowser().addEventListener("load",us.endrizzi.xdebugassistant.refresh, true);
		
		// Be sure to capture each new tab select to update the image
		var container = gBrowser.tabContainer;
		container.addEventListener("TabSelect", us.endrizzi.xdebugassistant.refresh, true);
	},
	
	setCookie: function (doc, cookieName, cookieVal,minutes) {
		var exp=new Date();
		exp.setTime(exp.getTime()+(minutes*60*1000));
		doc.cookie=cookieName+"="+cookieVal+"; expires="+exp.toGMTString()+"; path=/";
	},
	
	getCookie: function (doc,name) {
		var prefix = name + "="
		var cookieStartIndex = doc.cookie.indexOf(prefix)
		if (cookieStartIndex == -1)
				return null
		var cookieEndIndex = doc.cookie.indexOf(";", cookieStartIndex + prefix.length)
		if (cookieEndIndex == -1)
				cookieEndIndex = doc.cookie.length
		return unescape(doc.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
	}
};

window.addEventListener("load", us.endrizzi.xdebugassistant.init, false);